//余额提现
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    balance: '',
    money_num: '',
    gift: false,
  },

  // 全部提现功能
  all_click: function (e) {
    var that = this;
    that.setData({
      money_num: that.data.balance,
    })
  },
  // 提示提现金额
  import_input: function (e) {
    var that = this;
    console.log(e);
    var value = e.detail.value;
    if (value != '') {
      if (parseFloat(value) == 0) {
        that.setData({ gift: true, });
        setTimeout(function () { that.setData({ gift: false, }); }, 2000)
      }
    } else {
      that.setData({ money_num: '', })
    }
  },
  // 提现功能
  formSubmit: function (e) {
    console.log(e);
    var that = this;
    var vlaue_money = e.detail.value.money;
    var formId = e.detail.formId;
    var balance = that.data.balance;
    var openid = (wx.getStorageSync('openid'));
    console.log(vlaue_money);
    if (vlaue_money != '') {
      if (parseFloat(vlaue_money) == 0) {
        filing('请输入正确金额');
      } else {
        if (parseFloat(vlaue_money) < 1) {
          filing('到账金额不能低于1元');
        } else if (parseFloat(vlaue_money) > parseFloat(balance)) {
          filing('您的账户余额不足');
        } else {

          wx.showLoading({ title: '加载中', mask: true })
          console.log('发起请求');
          app.token_md5();
          wx.request({
            // url: 'https://hd.huwaishequ.com/red/public/tixian',
            // url: app.url2 + 'api/tixian',
            url: app.url2 + 'api/tixian',
            data: {
              openid: openid,
              price: vlaue_money,
              formId: formId,
              token: wx.getStorageSync('token_md5'),
            },
            header: { 'content-type': 'application/json' },
            success: function (res) {
              wx.hideLoading();
              var data = res.data;
              console.log(data);
              if (data == 'notenough') {
                filing('账户余额不足');
              } else if (data == 'chao') {
                filing('提现失败，当天提现次数已达上限');
              } else if (data == 'error') {
                filing('提现失败，请重试或联系管理员');
              } else if (data == 'success') {
                wx.showModal({
                  title: '提示',
                  content: '提现成功，1-5工作日到账',
                  success: function (res) {
                    if (res.confirm || res.cancel) {
                      console.log('用户点击确定');
                      balance -= vlaue_money;
                      that.setData({
                        balance: balance.toFixed(2),
                        money_num: '',
                      })
                    }
                  }
                })
              }

            },
            fail: function (res) {
              filing('提现失败', '请检查网络');
            },
          })
        }
      }
    } else {
      filing('提现失败', '请输入正确金额');
    }

    function filing(text) {
      wx.showModal({
        title: '提示',
        content: text,
      })
    }
  },
  //一分钱抽奖
  // minute_bolt: function () {
  //   wx.navigateToMiniProgram({
  //     appId: 'wx9bc23a213818b5d6',
  //     path: '/pages/group/lottery/lottery',
  //     extraData: {
  //       foo: 'bar'
  //     },
  //     envVersion: 'release',
  //     success(res) {
  //       // 打开成功
  //     }
  //   })
  // },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    wx.showLoading({ title: '加载中', mask: true })
    var that = this;
    app.token_md5();
    wx.request({
      url: app.url2 + 'api/yue',
      data: {
        openid: wx.getStorageSync('openid'),
        token: wx.getStorageSync('token_md5'),
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        that.setData({
          index: options.index,
          balance: res.data.money,
        })
        wx.hideLoading();        
      },
    })
  }

})